package com.linkin.homepage.base;

import android.view.View;

import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;

import java.util.HashMap;
import java.util.Map;

public class TabFragmentHelper {

    private int mContainerViewId = -1;
    private FragmentManager mFragmentManager = null;
    private Map<View, Fragment> mFragments = new HashMap<>();

    private View mCurrentTabView = null;
    private TabListener mTabListener = null;

    public interface TabListener {
        void onTabSelected(View tabView);
        void onTabUnselected(View tabView);
    }

    public TabFragmentHelper(FragmentManager fm, int containerViewId, TabListener listener) {
        mFragmentManager = fm;
        mContainerViewId = containerViewId;
        mTabListener = listener;
    }

    public TabFragmentHelper addTab(View tabView, Fragment fragment) {
        return addTab(tabView, fragment, null);
    }

    public TabFragmentHelper addTab(View tabView, Fragment fragment, Object tag) {
        if (tag != null) {
            tabView.setTag(tag);
        }
        mFragments.put(tabView, fragment);
        fragment.setUserVisibleHint(false);
        dispatchListener(tabView, null);
        return this;
    }

    public void clear() {
        mFragments.clear();
        mFragmentManager = null;
        mCurrentTabView = null;
        mContainerViewId = -1;
    }

    public View getCurrentTabView() {
        return mCurrentTabView;
    }

    public Fragment getCurrentFragment() {
        return getFragment(mCurrentTabView);
    }

    public Fragment getFragment(View tabView) {
        return tabView != null ? mFragments.get(tabView) : null;
    }

    public TabFragmentHelper switchTab(View tabView) {
        Fragment fragment = getFragment(tabView);
        if (fragment == null || mCurrentTabView == tabView) {
            return this;
        }

        FragmentTransaction ft = mFragmentManager.beginTransaction();
        Fragment lastFragment = getCurrentFragment();
        if (lastFragment != null) {
            ft.hide(lastFragment);
        }
        if (fragment.isAdded()) {
            ft.show(fragment);
        } else {
            ft.add(mContainerViewId, fragment);
        }
        ft.commitAllowingStateLoss();

        if (lastFragment != null) {
            lastFragment.setUserVisibleHint(false);
        }
        fragment.setUserVisibleHint(true);

        View oldTabView = mCurrentTabView;
        mCurrentTabView = tabView;
        dispatchListener(oldTabView, tabView);
        return this;
    }

    private void dispatchListener(View unselectedTabView, View selectedTabView) {
        if (mTabListener != null) {
            if (unselectedTabView != null) {
                mTabListener.onTabUnselected(unselectedTabView);
            }
            if (selectedTabView != null) {
                mTabListener.onTabSelected(selectedTabView);
            }
        }
    }
}
